﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Neeko.XNA.UI
{
    public enum FrameOrientation
    {
        Horizontal,
        Vertical
    }

    public class AnimatedSprite : Sprite
    {
        private int _frameCount;
        public int FrameCount
        {
            get { return _frameCount; }
            set
            {
                if (value < 1)
                    _frameCount = 1;
                else
                    _frameCount = value;

                UpdateSourceRectangle();
            }
        }

        private double _framesPerSecond;
        public double FramesPerSecond
        {
            get { return _framesPerSecond; }
            set
            {
                if (value < 0)
                    _framesPerSecond = 0;
                else
                    _framesPerSecond = value;
            }
        }

        public FrameOrientation FrameOrientation { get; set; }

        private int _currentFrame;
        public int CurrentFrame
        {
            get { return _currentFrame; }
            set
            {
                if (value < 0)
                    _currentFrame = 0;
                else
                    _currentFrame = value % FrameCount;

                UpdateSourceRectangle();
            }
        }

        double _elapsedMiliseconds;

        public AnimatedSprite(Texture2D texture)
            : base(texture)
        {
            this.FrameOrientation = FrameOrientation.Vertical;
            _frameCount = 1;
            _currentFrame = 0;
            _elapsedMiliseconds = 0;
            _framesPerSecond = 0;
        }

        public void Update(GameTime gameTime)
        {
            _elapsedMiliseconds += gameTime.ElapsedGameTime.TotalMilliseconds;

            int previousFrame = _currentFrame;
            while (this.FramesPerSecond * 1000 >= _elapsedMiliseconds)
            {
                _elapsedMiliseconds -= this.FramesPerSecond * 1000;
                _currentFrame++;
            }
            _currentFrame %= this.FrameCount;

            if (previousFrame != _currentFrame)
            {
                UpdateSourceRectangle();
            }
        }

        void UpdateSourceRectangle()
        {
            if (this.FrameOrientation == UI.FrameOrientation.Horizontal)
            {
                double frameWidth = base.Texture.Width / (double)_frameCount;
                double xOffset = frameWidth * _currentFrame;
                base.SourceRectangle = new Rectangle((int)xOffset, 0, (int)frameWidth, base.Texture.Height);
            }
            else
            {
                double frameHeight = base.Texture.Height / (double)_frameCount;
                double yOffset = frameHeight * _currentFrame;
                base.SourceRectangle = new Rectangle(0, (int)yOffset, base.Texture.Width, (int)frameHeight);
            }
        }
    }
}
